<?php
try{
    include '../ini.php';
    if(empty($_GET['id'])){
        throw new Exception('Mã sản phẩm này không tồn tại');
    }
    $read_product=$dt_xl_product->read_id($_GET['id']);
    if($read_product===FALSE){
        throw new Exception('Không có sản phẩm này');
    }
    $quantity=1;
    $kq='';
    if($read_product['price_old'] > 0){
        if($read_product['price_old'] < $read_product['price_new']){
            $kq=$read_product['price_new'] - $read_product['price_old'];
        }else{
            $kq=$read_product['price_old'] - $read_product['price_new'];
        }
    }
    $read_brand_product=$dt_xl_product->read_brand_product($read_product['brand_products_id']);
    /*if($read_brand_product == NULL){
        throw new Exception('Không có thương hiệu sản phẩm này');
    }*/
    $read_product_images=$dt_xl_product->read_product_image($_GET['id']);
    $list_review=$dt_xl_product->read_alias($_GET['id']);
    $sum=0;
    $count=0;
    $comments=0;
    foreach($list_review as $value){
        $sum+=$value['review'];
        $count+=count($value['review']);
        $comments+=count($value['product_id']);
    }
    if(!empty($list_review)){
        $percent=($sum/$count)*7.2;
    }else{
        $percent='0';
        $comments='0';
    }
    
    /* SIZE */
    $list_product_id=$dt_xl_product->list_product($_GET['id']);
    foreach($list_product_id as $product){
        $list_size=explode(',',$product['size']);
    }
    
    /* ASSIGN*/ 
    $dt_smarty->assign('list_size',$list_size);
    $dt_smarty->assign('list_review',$list_review);
    $dt_smarty->assign('percent',$percent);
    $dt_smarty->assign('comments',$comments);
    $dt_smarty->assign('kq',$kq);
    $dt_smarty->assign('read_brand_product',$read_brand_product);
    $dt_smarty->assign('read_product',$read_product);
    $dt_smarty->assign('read_product_images',$read_product_images);
    $dt_smarty->assign('quantity',$quantity);
    $contentForLayout=$dt_smarty->fetch('products/detail.tpl');
    $dt_smarty->assign('titleForLayout','Sản Phẩm');
    $dt_smarty->assign('contentForLayout',$contentForLayout);
    $dt_smarty->display('layouts/default.tpl');
}catch(Exception $e){ 
    $dbh=NULL;
    $_SESSION['msg']=$e->getMessage();
    $_SESSION['type_msg']='error';
    header("Location: /");
    
}